package com.company.aicrawlers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.company.aicrawlers.dto.AnalysisTaskVO;
import com.company.aicrawlers.entity.*;
import com.company.aicrawlers.mapper.*;
import com.company.aicrawlers.service.AnalysisQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 分析查询服务实现
 *
 * @author AI Crawlers Team
 * @date 2025-10-11
 */
@Slf4j
@Service
public class AnalysisQueryServiceImpl implements AnalysisQueryService {

    @Autowired
    private AnalysisTaskMapper analysisTaskMapper;

    @Autowired
    private AnalysisTaskSummaryMapper taskSummaryMapper;

    @Autowired
    private AnalysisProductDetailMapper productDetailMapper;

    @Autowired
    private AnalysisQuestionResultMapper questionResultMapper;

    @Autowired
    private CollectTaskMapper collectTaskMapper;

    @Override
    public List<AnalysisTaskVO> getAnalysisTaskList() {
        log.info("查询分析任务列表");
        
        // 1. 查询所有分析任务
        List<AnalysisTask> tasks = analysisTaskMapper.selectList(
            new LambdaQueryWrapper<AnalysisTask>()
                .orderByDesc(AnalysisTask::getCreateTime)
        );
        
        List<AnalysisTaskVO> voList = new ArrayList<>();
        
        for (AnalysisTask task : tasks) {
            AnalysisTaskVO vo = new AnalysisTaskVO();
            
            // 2. 复制基本信息
            BeanUtils.copyProperties(task, vo);
            
            // 3. 查询采集任务名称
            CollectTask collectTask = collectTaskMapper.selectById(task.getCollectTaskId());
            if (collectTask != null) {
                vo.setCollectTaskName(collectTask.getTaskName());
            }
            
            // 4. 查询汇总信息
            AnalysisTaskSummary summary = taskSummaryMapper.selectOne(
                new LambdaQueryWrapper<AnalysisTaskSummary>()
                    .eq(AnalysisTaskSummary::getAnalysisId, task.getAnalysisId())
            );
            
            if (summary != null) {
                vo.setTotalPlatforms(summary.getTotalPlatforms());
                vo.setTotalQuestions(summary.getTotalQuestions());
                vo.setTotalProducts(summary.getTotalProducts());
                vo.setOurProducts(summary.getOurProductCount());
                vo.setCompetitorProducts(summary.getCompetitorCount());
                // 计算我方产品占比
                if (summary.getTotalProducts() != null && summary.getTotalProducts() > 0) {
                    BigDecimal ratio = BigDecimal.valueOf(summary.getOurProductCount() * 100.0 / summary.getTotalProducts())
                        .setScale(2, java.math.RoundingMode.HALF_UP);
                    vo.setOurProductRatio(ratio);
                }
                vo.setOurProductAvgRanking(summary.getOurProductAvgRanking());
            }
            
            voList.add(vo);
        }
        
        log.info("查询到{}个分析任务", voList.size());
        return voList;
    }

    @Override
    public AnalysisTask getAnalysisTask(Long analysisId) {
        log.info("查询分析任务：analysisId={}", analysisId);
        return analysisTaskMapper.selectById(analysisId);
    }

    @Override
    public AnalysisTaskSummary getTaskSummary(Long analysisId) {
        log.info("查询任务汇总：analysisId={}", analysisId);
        return taskSummaryMapper.getByAnalysisId(analysisId);
    }

    @Override
    public List<AnalysisProductDetail> getProductList(Long analysisId) {
        log.info("查询商品列表：analysisId={}", analysisId);
        return productDetailMapper.getByAnalysisId(analysisId);
    }

    @Override
    public AnalysisProductDetail getProductDetail(Long analysisId, Long productId) {
        log.info("查询商品详情：analysisId={}, productId={}", analysisId, productId);
        return productDetailMapper.getByAnalysisIdAndProductId(analysisId, productId);
    }

    @Override
    public List<AnalysisQuestionResult> getQuestionList(Long analysisId) {
        log.info("查询问句列表：analysisId={}", analysisId);
        return questionResultMapper.getByAnalysisId(analysisId);
    }

    @Override
    public AnalysisQuestionResult getQuestionDetail(Long analysisId, Long questionId) {
        log.info("查询问句详情：analysisId={}, questionId={}", analysisId, questionId);
        return questionResultMapper.getByAnalysisIdAndQuestionId(analysisId, questionId);
    }
}

